<?php
/** ************************
	Php Quran - 2009
Copyrights for all muslims
***************************/

/*
* header of phpquran
* to show header in any page you want .. 
*  parameter : title : title of page as in <titl></titl>
*/	
function Saaheader($title, $outscript=false)
{
		global $tpl, $config, $lang;

		$tpl->assign("title", $title);
		$tpl->assign("go_back_browser", $lang['BACK']);
	
		$header = $tpl->display("header");
		
		if($config['siteclose'] == '1' && is_admin())
		{
			echo '<div style="width: 100%; text-align:center; background:#FFFFA6; color:black; border:thin; border-color:orange">' . $lang['SITE_CLOSED'] . '</div>
';
		}
		
		echo $header;
}


/*
*footer
* to show footer of any page you want 
* paramenters : none
*/
function Saafooter()
{
		global $tpl, $SQL, $config, $lang;
						
		$footer = $tpl->display("footer");
		echo $footer;
		
		// THEN .. at finish, close sql connections
		$SQL->close();
}

/*
* print inforamtion message 
* parameters : msg : text that will show as inforamtion
					title : <title>title of page</title>
					exit : stop script after showing msg 
*/
function phpquran_info($msg,$title='', $exit=true, $redirect=false, $rs='2')
{
	global $text, $tpl, $SQL, $lang;

	// assign {text} in info template
	$text	= $msg;
	//header
	Saaheader($title);
	//show tpl
	echo $tpl->display('info');
	//footer
	Saafooter();
	
	//redirect
	if($redirect)
	{
        echo '<meta http-equiv="refresh" content="' . $rs . ';url=' . $redirect . '" />'; 
	}
	
	if ($exit)
	{
		$SQL->close();
		exit();
	}
}

/*
* print error message 
* parameters : msg : text that will show as error mressage
					title : <title>title of page</title>
					exit : stop script after showing msg 
*/
function phpquran_err($msg, $title='', $exit=true, $redirect=false, $rs='2')
{
	global $text, $tpl, $SQL, $lang;
				
	// assign {text} in err template
	$text	= $msg;
	//header
	Saaheader($title);
	//show tpl
	echo $tpl->display('err');
	//footer
	Saafooter();

	//redirect
	if($redirect)
	{
        echo '<meta http-equiv="refresh" content="' . $rs . ';url=' . $redirect . '" />'; 
	}

	if ($exit)
	{
		$SQL->close();
		exit();
	}
}


/*
* show error of critical problem !
* parameter: error_title : title of prblem
*					msg_text: message of problem
*/
function big_error ($error_title,  $msg_text)
{
	global $SQL; 
	echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">' . "\n";
	echo '<head>' . "\n";
	echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n";
	echo '<title>' . htmlspecialchars($error_title) . '</title>' . "\n";
	echo '<style type="text/css">' . "\n\t";
	echo '* { margin: 0; padding: 0; }' . "\n\t";
	echo 'body { background: #fff;color: #444;font-family:tahoma, verdana, arial, sans-serif;font-size: 11px;margin: 0 auto;padding: 50px;width: 767px;}' . "\n\t";
	echo '.error {color: #333;background:#ffebe8;border: 1px solid #dd3c10;}' . "\n\t";
	echo '.error {padding: 10px;font-family:"lucida grande", tahoma, verdana, arial, sans-serif;font-size: 12px;}' . "\n";
	echo '</style>' . "\n";
	echo '</head>' . "\n";
	echo '<body>' . "\n\t";
	echo '<div class="error">' . "\n";
	echo "\n\t\t<h2>phpquran Error : </h2><br />" . "\n";
	echo "\n\t\t<strong> [ " . $error_title . ' ] </strong><br /><br />' . "\n\t\t" . $msg_text . "\n\t";
	echo "\n\t\t" . '<br /><br />' . "\n\t";
	echo '</div>' . "\n";
	echo '</body>' . "\n";
	echo '</html>';
	@$SQL->close();
	exit();
}

//redirect [php.net]
function redirect($url, $header=true, $exit=false)
{
	global $SQL;
	
    if (!headers_sent() && $header)
	{
        header('Location: ' . $url); 
    }
	else
	{
        echo '<script type="text/javascript">window.location.href="' . $url . '";</script>';
        echo '<noscript><meta http-equiv="refresh" content="0;url=' . $url . '" /></noscript>'; 
	}
	
	$SQL->close();
	
	if($exit)
	{
		exit;
	}
}

function get_ip()
{
	$ip = '';
	if(!empty($_SERVER['REMOTE_ADDR']))
	{
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	else if (!empty($_SERVER['HTTP_CLIENT_IP']))
	{
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	}
	else 
	{
		$ip = getenv('REMOTE_ADDR');
		if(getenv('HTTP_X_FORWARDED_FOR'))
		{
			if(preg_match("/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/", getenv('HTTP_X_FORWARDED_FOR'), $ip3))
			{
				$ip2 = array('/^0\./', '/^127\.0\.0\.1/', '/^192\.168\..*/', '/^172\.16\..*/', '/^10..*/', '/^224..*/', '/^240..*/');
				$ip = preg_replace($ip2, $ip, $ip3[1]);
			}
		}
	}
	
	return preg_replace('/[^0-9a-z.]/i', '', $ip);
}

function phpquranOnline ()
{
	global $SQL, $dbprefix, $config, $session;
	
	// get information .. 
	$ip				= get_ip();
	$agent			= $SQL->escape($_SERVER['HTTP_USER_AGENT']);
	$timeout		= 600; //seconds //10 min
	$time			= time();  
	$timeout2		= $time-$timeout;  
	$username		= /**($usrcp->name()) ? $usrcp->name(): **/ '-1';
						
	//---
	if(!empty($ip) && !empty($agent) && !empty($session))
	{
		$rep_query = array(
							'REPLACE'	=> 'ip, username, agent, time, session',
							'INTO'		=> "{$dbprefix}online",
							'VALUES'	=> "'$ip','$username','$agent','$time','$session'",
							'UNIQUE'	=>  "session='$session'"
						);
		$SQL->build($rep_query);
	}
		
	$last_online_time_update = get_cache_value('last_online_time_update');
		
	//clean online table
	if((time() - $last_online_time_update) >= 3600)
	{
		$query_del = array(
						'DELETE'	=> "{$dbprefix}online",
						'WHERE'		=> "time < '$timeout2'"
					);								
		$SQL->build($query_del);
			
		//update last_online_time_update 
		update_cache('last_online_time_update', time());
	}
		
}#End function

function get_cache_value ($cache)
{
	global $SQL, $dbprefix;
	
	$query = $SQL->build(array(
		'SELECT'	=> 'value',
		'FROM'		=> "{$dbprefix}cache",
		'WHERE'		=> 'name = \'' . $SQL->escape($cache) . '\'',
		));
		
	while($row = $SQL->fetch_array($query))
	{
		return $row['value'];
	}
	$SQL->freeresult($row);
}

//delete cache
function delete_cache($name, $all=false)
{
	
	$path_to_cache = PATH . './cache';
	
	if($all)
	{
		$dh = @opendir($path_to_cache);
		while (($file = @readdir($dh)) !== false)
		{
			if($file != "." && $file != ".." && $file != ".htaccess" && $file != "index.html" && $file != "php.ini" && $file != 'styles_cached.php')
			{
				$del = @unlink($path_to_cache . '/' . $file, true);
			}
		}
		@closedir($dh);
	}
	else
	{
		$del = true;
		$name = str_replace('.php', '', $name) . '.php';
		if (file_exists($path_to_cache . '/' . $name))
		{
			$del = @unlink ($path_to_cache . "/" . $name, true);
		}
	}
	
	return $del;
}

function update_cache($name, $value, $escape = true)
{
	global $SQL, $dbprefix;
	
	$value = ($escape) ? $SQL->escape($value) : $value;
	
	$update_query = array(
						'UPDATE'	=> "{$dbprefix}cache",
						'SET'		=> "value='" . ($escape ? $SQL->escape($value) : $value) . "'",
						'WHERE'		=> 'name = "' . $SQL->escape($name) . '"'
				);
				
	$SQL->build($update_query);
	
	if($SQL->affected())
	{
		return true;
	}
	else
	{
		return false;
	}
}
#<-- EOF